<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 *  Notice Model
 *
 * @package XGO CMS v3.0
 * @subpackage Notice
 * @link http://sunsoft.vn
 */

class BookingDebts_model extends MY_Model{

	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_list_booking';
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
	public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => false
				),
				array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				)
                                ,array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				)
                                ,array(
						'name' 	=> 'airline',
						'label' => $this->lang->line('airline'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'ship_name_customer',
						'label' => $this->lang->line('ship_name_customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'ship',
						'label' => $this->lang->line('ship'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => false
				)
                                ,array(
						'name' 	=> 'payment_method',
						'label' => $this->lang->line('payment_method'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' 	=> 'net_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'sale_fare',
						'label' => $this->lang->line('sale_fare'),
						'width' => '3%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'payment_confirm',
						'label' => $this->lang->line('payment_confirm'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'createdTime',
						'label' => $this->lang->line('createdTime'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label'  => false,
						'searchoptions' => false
				)
		);
	}
	
	/*
	 * datatables data
	* return json
	*/
	public function json_data($controller, $right){
            
                if($this->session->userdata('h_bookingDebts')){
                    $arr_status = json_decode($this->session->userdata('h_bookingDebts'));
                }
            
		$this->datatables
		->select('c.id,c.customerID, c.booking_place_code, c.airline, c.booking, c.booking_customer_name, c.net_fare, c.sale_fare,c.payment_confirm,c.createdTime,method.payment_name,usr.username,bc.name AS khachhang')
		->from($this->table_name.' AS c')
                ->join('users AS usr','usr.id = c.owner')
                ->join('book_payment_method AS method','method.id = c.payment_method')
                ->join('book_contacts AS bc','bc.customerID = c.customerID')
                ->where('c.owner',$this->session->userdata['user_id'])
                ->where('c.payment_method','4');
                
                if(isset($arr_status) && $arr_status->h_booking_place_code != ''){
                    $this->datatables->where('c.booking_place_code ',$arr_status->h_booking_place_code);
                }
                if(isset($arr_status) && $arr_status->h_beginTime != ''){
                    $this->db->where('c.createdTime BETWEEN '.$arr_status->h_beginTime. ' AND '.$arr_status->h_endTime);
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData']    = array();
                
                $this->session->unset_userdata('h_bookingDebts');

		foreach($datatables['aaData'] as $item){
                        $booking            = json_decode($item['booking']);
                        $string             = '';
                        $i                  = 0;
                        while($i < count($booking)){
                            $string .= $booking[$i].'<br/>';
                            $i++;
                        }
			$ouput['aaData'][] = array(
                                        $item['id'],
					$item['khachhang'],
                            		$item['booking_place_code'],
                                        $item['airline'],
                                        $this->bookinglib->getFirstCustomer($item['booking_customer_name']),
                                        $string,                                        
                                        $item['payment_name'],  
                                        $this->bookinglib->my_number_format($item['net_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','),
                                        ($item['payment_confirm'] == 1)?'Đã thanh toán':'Chưa thanh toán',
                                        date("d-m-Y H:i:s",$item['createdTime']), 
                                        $item['username'],
					$this->add_button_status("bookingStatus", $right, $item['id'])
			);
		}
	
		return json_encode($ouput);
	}
}

/* End of file listcustomers_model.php */
/* Location: ./application/models/listcustomers_model.php */